package com.chengxiaoxiao.ai_demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chengxiaoxiao.ai_demo.api.ConversationInfoControllerApi;
import com.chengxiaoxiao.ai_demo.entity.ConversationInfo;
import com.chengxiaoxiao.ai_demo.entity.OriginalMessage;
import com.chengxiaoxiao.ai_demo.service.ConversationInfoService;
import com.chengxiaoxiao.ai_demo.service.MessageInfoService;
import com.chengxiaoxiao.ai_demo.vo.PageResult;
import com.chengxiaoxiao.ai_demo.vo.Result;
import com.chengxiaoxiao.ai_demo.vo.form.ChatForm;
import com.chengxiaoxiao.ai_demo.vo.query.ConversationInfoQuery;
import com.chengxiaoxiao.ai_demo.vo.vo.ChatResponseVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * 聊天会话相关接口
 *
 * @Description:
 * @Author Cheng Xiaoxiao
 * @Date: 2025-3-12 15:54:33
 */
@RestController
@RequestMapping("/conversation-info")
@RequiredArgsConstructor
public class ConversationInfoController implements ConversationInfoControllerApi {
    final ConversationInfoService conversationInfoService;
    final MessageInfoService messageInfoService;

    /**
     * 获取条件查询信息
     *
     * @param conversationInfoQuery 查询条件
     * @return 查询封装
     */
    private LambdaQueryWrapper<ConversationInfo> getApplicationInfoQueryWrapper(ConversationInfoQuery conversationInfoQuery) {
        LambdaQueryWrapper<ConversationInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(conversationInfoQuery.getTitle()), ConversationInfo::getTitle,
                conversationInfoQuery.getTitle());
        return wrapper;
    }

    /**
     * 按照条件查询会话信息
     *
     * @param conversationInfoQuery 查询条件
     * @return 条件结果信息
     */
    @Override
    @GetMapping
    public Result<List<ConversationInfo>> list(ConversationInfoQuery conversationInfoQuery) {
        return Result.success(conversationInfoService.list(getApplicationInfoQueryWrapper(conversationInfoQuery)));
    }

    /**
     * 分页及条件查询会话信息
     *
     * @param page                  页码
     * @param size                  分页大小
     * @param conversationInfoQuery 查询条件
     * @return 分页结果信息
     */
    @Override
    @GetMapping("/{size}/{page}")
    public Result<PageResult<ConversationInfo>> list(@PathVariable("size") Long size,
                                                     @PathVariable("page") Long page,
                                                     ConversationInfoQuery conversationInfoQuery) {
        if (page <= 0) {
            page = 1L;
        }
        if (size <= 0) {
            size = 10L;
        }
        Page<ConversationInfo> pageParam = new Page<>(page, size);

        conversationInfoService.page(pageParam, getApplicationInfoQueryWrapper(conversationInfoQuery));
        PageResult<ConversationInfo> pageResult = new PageResult<>(pageParam.getTotal(), pageParam.getSize(),
                pageParam.getCurrent(), pageParam.getRecords());

        return Result.success(pageResult);
    }

    /**
     * 创建新的会话
     *
     * @return 会话信息
     */
    @Override
    @PostMapping
    public Result<ConversationInfo> create() {
        return Result.success(conversationInfoService.create());
    }

    /**
     * 编辑会话
     */
    @Override
    @PutMapping("/{id}")
    public Result<ConversationInfo> edit(@PathVariable("id") String id, @RequestBody ConversationInfo conversationInfo) {
        return Result.success(conversationInfoService.editById(id, conversationInfo));
    }

    /**
     * 删除会话
     */
    @Override
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable("id") String id) {
        conversationInfoService.deleteById(id);
        return Result.success(true);
    }

    /**
     * 进行聊天
     *
     * @param id       会话ID
     * @param chatForm 会话表单
     * @return 回答结果
     */
    @Override
    @PostMapping(value = "/chat/{id}", produces = MediaType.APPLICATION_NDJSON_VALUE)
    public Flux<ChatResponseVo> chatStream(@PathVariable("id") String id, @RequestBody ChatForm chatForm) {
        return conversationInfoService.chatStream(id, chatForm);
    }

    /**
     * 根据会话ID查询历史记录
     *
     * @return 聊天历史记录
     */
    @GetMapping("/message/{id}")
    @Override
    public Result<List<OriginalMessage>> listConversationMessage(@PathVariable("id") String id) {
        return Result.success(messageInfoService.listMessageByConversationId(id));
    }
}
